
local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.BaseComponent());

local ServerEventManager = ServerOHOH.load.ServerEventManager();
local EventList = ServerOHOH.load.EventList();

local TRADE_LIST={}

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
end;

function M.dtor(self)
	M.super.dtor(self);
end;

-- 交易请求发送
ServerEventManager:RegistHandle(EventList.SendTradeAsk, function(player,userId)
	 local accepter= ServerOHOH.load.Server_Player().FindPlayerByUserId(userId)
	 if not accepter then
		return
	 end	
	 assert(accepter,"accepter==null")
	 
	 local ctrl=accepter:GetComponent(_G.CommonOHOH.load.Common_TradeCtrl())
	 local ctrl2=player:GetComponent(_G.CommonOHOH.load.Common_TradeCtrl())

	if ctrl and ctrl2 and ctrl:IsCanSendAsk(player) and ctrl2:IsCanTrade(accepter)  then

		local trade=ServerOHOH.load.Server_Trade().new(player,accepter)
		local tid=trade:GetId()
		TRADE_LIST[tid]=trade
		accepter:Fire(EventList.SendTradeAsk_Client,tid,player:GetUserId())

		trade.DtorAction:Connect(function(id)
			TRADE_LIST[id]=nil
		end)
	end

end)

ServerEventManager:RegistHandle(EventList.AcceptTradeAsk, function(player,tradeId,isAccept)
	if not player then
		return
	end

	if not TRADE_LIST[tradeId] then
		return
	end

	assert(player,"player==null")
	assert(TRADE_LIST[tradeId],"trade==null:"..tradeId)
	local trade=TRADE_LIST[tradeId]
	if isAccept and trade:IsCanTrade() then
		trade:StartTrade()
	else
		trade:dtor()
	end
end)


return M;

